//Holds the major functions and such that govern the control of resources including gathering resources

include('SQL.lua');
include('playerinvtracker.lua');

local Nodes = {};
local Resources = {};
local Sweps = {};
local info = {};

table.insert( Sweps, "weapon_knife", 3 );


function updateNodeData()
	Nodes = ents.FindByClass( "resource_node" );
	
	for k,v in pairs( Nodes ) do
		Resources[ k ] = v:GetNetworkedInt( "ResourceAmmount" ) );
	end

end

function doTake( hitnode, callingswep )

	local NodeKey = table.KeyFromValue( Nodes, hitnode:GetClass() );
	local NodeResources = Resources[ NodeKey ];
	local CallingSwepPower = Sweps[ callingswep:GetClass() ];
	local NodeType = hitnode:GetNetworkedString( "Type" );

	if table.HasValue( Nodes, hitnode ) then
		
		local Outcome = math.random( 0, NodeResources-CallingSwepPower );
		
		if Outcome <= NodeResources/4 then return false;
		else 
			Yield = (NodeResources/4)+Outcome;
			hitnode:SetNetworkedInt( NodeResources-Yield );
			
			if NodeType == "rock" then;
				
				info[ "use" ] = false;
				info[ "drop" ] = true;
				
				local ResourceTable = calculateMinerals( Yield, CallingSwepPower );
				
				info[ "ammount" ] = ResourceTable[ "granite" ];
				giveitem( "Granite", info );
				
				if ResourceTable[ "copper" ] != nil then
					info[ "ammount" ] = ResourceTable[ "copper" ];
					giveitem( "Copper", info );
				end
				
				if ResourceTable[ "steel" ] != nil then
					info[ "ammount" ] = ResourceTable[ "steel" ];
					giveitem( "Steel", info );
				end
				
				if ResourceTable[ "titanium" ] != nil then
					info[ "ammount" ] = ResourceTable[ "titanium" ];
					giveitem( "Titanium", info );
				end
				
			else
				info[ "ammount" ] = Yield;
				info[ "use" ] = false;
				info[ "drop" ] = true;
				giveitem( "Wood", info );
			end
				
			if NodeResources-Yield <= 0 then
				hitnode:Remove();
			end
				
			return Yield;
			
		end
		else return false; 
	end
end

function calculateMinerals( yield, swep )

	local ResourceTier = 0;
	local ResourceYieldType = {};
	
	if swep > 5 then
		ResourceTier = math.random( 0, 1 );
	elseif swep > 10 then;
		ResourceTier = math.random( 0, 2 );
	else 
		ResourceTier = math.random( 0, 3 ); 
	end

	
	if ResourceTier == 0 then
		ResourceYieldType[ "granite" ] = yield;
	elseif ResourceTier == 1 then
		local Granite = math.random( math.Round( yield/(5+(swep/2)) ), math.Round( yield/2 ) );
		local Copper = yield - Granite;
		ResourceYieldType[ "granite" ] = Granite;
		ResourceYieldType[ "copper" ] = Copper;
	elseif ResourceTier == 2 then
		local Granite = math.random( math.Round( yield/(6+(swep/3)) ), math.Round( yield/3 ) );
		local Copper = math.random( math.Round( (yield-Granite)/(4+(swep/2)) ), math.Round( (yield-Granite)/2 ) );
		local Steel = yield-Granite-Copper;
		ResourceYieldType[ "granite" ] = Granite;
		ResourceYieldType[ "copper" ] = Copper;
		ResourceYieldType[ "steel" ] = Steel;
	else
		local Granite = math.random( math.Round( yield/(7+(swep/4)) ), math.Round( yield/4 ) );
		local Copper = math.random( math.Round( (yield-Granite)/(5+(swep/3)) ), math.Round( (yield-Granite)/3 ) );
		local Steel = math.random( math.Round( (yield-Granite-Copper)/(4+(swep/2)) ), math.Round( (yield-Granite)/2 ) );
		local Titanium = yield-Granite-Copper-Steel;
		ResourceYieldType[ "granite" ] = Granite;
		ResourceYieldType[ "copper" ] = Copper;
		ResourceYieldType[ "steel" ] = Steel;
		ResourceYieldType[ "titanium" ] = Titanium;
	end
	return ResourceYieldType; 
end
	
		